/**
 * Storage 模块 API
 * 存储配置相关接口
 */

import { request, type ApiResponse } from "../request";

// ============ 类型定义 ============

/**
 * 存储特性配置
 */
export interface StorageFeatures {
	imageTransformation: {
		enabled: boolean;
	};
	s3Protocol: {
		enabled: boolean;
	};
	icebergCatalog: {
		enabled: boolean;
		maxNamespaces: number;
		maxTables: number;
		maxCatalogs: number;
	};
	vectorBuckets: {
		enabled: boolean;
		maxBuckets: number;
		maxIndexes: number;
	};
}

/**
 * 存储能力配置
 */
export interface StorageCapabilities {
	list_v2: boolean;
	iceberg_catalog: boolean;
}

/**
 * 存储外部配置
 */
export interface StorageExternal {
	upstreamTarget: "main" | "canary";
}

/**
 * 存储配置
 */
export interface StorageConfig {
	fileSizeLimit: number;
	features: StorageFeatures;
	capabilities: StorageCapabilities;
	external: StorageExternal;
	migrationVersion: string;
	databasePoolMode: string;
}

/**
 * 更新存储配置请求
 */
export interface UpdateStorageConfigBody {
	fileSizeLimit?: number;
	features?: {
		imageTransformation?: {
			enabled: boolean;
		};
		s3Protocol?: {
			enabled: boolean;
		};
		icebergCatalog?: {
			enabled: boolean;
			maxNamespaces: number;
			maxTables: number;
			maxCatalogs: number;
		};
		vectorBuckets?: {
			enabled: boolean;
			maxBuckets: number;
			maxIndexes: number;
		};
	};
	external?: {
		upstreamTarget: "main" | "canary";
	};
}

/**
 * 存储桶信息
 */
export interface StorageBucket {
	id: string;
	name: string;
	owner: string;
	created_at: string;
	updated_at: string;
	public: boolean;
}

// ============ API 方法 ============

/**
 * 获取项目存储配置
 */
export function getStorageConfig(ref: string): Promise<ApiResponse<StorageConfig>> {
	return request.get<StorageConfig>(`/v1/projects/${ref}/config/storage`);
}

/**
 * 更新项目存储配置
 */
export function updateStorageConfig(
	ref: string,
	data: UpdateStorageConfigBody
): Promise<ApiResponse<Record<string, never>>> {
	return request.patch<Record<string, never>>(`/v1/projects/${ref}/config/storage`, data);
}

/**
 * 获取所有存储桶
 */
export function listBuckets(ref: string): Promise<ApiResponse<StorageBucket[]>> {
	return request.get<StorageBucket[]>(`/v1/projects/${ref}/storage/buckets`);
}

export default {
	getStorageConfig,
	updateStorageConfig,
	listBuckets
};
